setlocal enabledelayedexpansion
cd /d %~dp0
set path=%path%;%cd%

set arg2=%2
set arg2=%arg2:/=\%

set pids=%arg2%-1.%random%
cat %1 >%pids%
call treeps.bat %1 %pids%
set totalmem=0
del %arg2%
for /f %%p in (%pids%) do  (
if not "x%%p" == "x" (
   echo [ProcessId=%%p] >>%arg2%
   wmic process where "(ProcessId=%%p)" get ParentProcessId,WorkingSetSize,CommandLine /format:list | more >%pids%.mem
   cat %pids%.mem     >>%arg2%
   netstat -nao | grep -Pi "listening" | grep -P "\s%%p$"| grep -Pio "^\s*\S+\s+\S+:\d+" | grep -Po "\S+:\d+$" >%pids%.port
   for /f %%t in (%pids%.port) do echo port=%%t >>%arg2%
   grep -Po "WorkingSetSize=\d+" %pids%.mem | grep -Po "\d+" >%pids%.mem1
   set /p mem1=<%pids%.mem1
   rem echo mem1=!mem1!
   set /a totalmem=!totalmem!+!mem1!/1024
   rem echo memory=!totalmem!KB
   echo memory=!totalmem!KB >%pids%.totalmem
   )
)

if EXIST %pids%.totalmem (
  echo [total] >>%arg2%
  cat %pids%.totalmem >>%arg2%
)

del %pids%.*

:end
